package org.hyl.javaext.disruptor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import com.lmax.disruptor.RingBuffer;

public class DisruptorProducer 
{
    private static final String FINIDHED = "EOF";
    private final RingBuffer<FileData> ringBuffer;

    public DisruptorProducer(RingBuffer<FileData> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void pushData(String line) {
        long seq = ringBuffer.next();
        try {
            FileData event = ringBuffer.get(seq);   // ��ȡ����λ��
            event.setLine(line);                    // ������λ��
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ringBuffer.publish(seq);        // ֪ͨ������
        }
    }

    public void read(String fileName) {
        try {
        	File fi=new File(fileName);
        	File f = new File(".");
        	String absolutePath = f.getAbsolutePath();
        	System.out.println(absolutePath);
            BufferedReader reader = new BufferedReader(new FileReader(fi));
            String line;
            while ((line = reader.readLine()) != null) {
                // ��������
               	System.out.println(line);
                pushData(line);
            }
            // ������־
            pushData(FINIDHED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}